package ch.sfug.display {
	import flash.display.Sprite;	

	/**
	 * @author loop
	 */
	public class Pane extends Sprite {

		public var content:Sprite;
		public var masker:Sprite;

		public function Pane() {
			
			if( content == null ) content = new Sprite();
			
			if( masker == null ) {
				masker = new Sprite();
				masker.graphics.lineStyle(0);
				masker.graphics.beginFill(0);
				masker.graphics.drawRect(0, 0, 300, 200);
				masker.graphics.endFill();
			}
			
			if( !contains( content ) ) addChild( content );
			if( !contains( masker ) ) addChild( masker );
			content.mask = masker;
		}
		
		/**
		 * returns a number between 0 and 1 that represents the position of the content. 0 fully left, 1 fully right.
		 */
		public function get panX( ):Number {
			if( deltaX > 0 ) {
				return -content.x / deltaX;
			} else {
				return 0;
			} 
		}

		
		
		/**
		 * returns a number between 0 and 1 that represents the position of the content. 0 fully left, 1 fully right.
		 */
		public function get panY( ):Number {
			if( deltaY > 0 ) {
				return -content.y / deltaY;
			} else {
				return 0;
			} 
		}

		
		/**
		 * sets the position of the content where 0 is fully left and 1 is fully right.
		 */
		public function set panX( px:Number ):void {
			content.x = Math.max(0, Math.min(px, 1)) * -deltaX;
		}

		
		/**
		 * sets the position of the content where 0 is fully top and 1 is fully bottom.
		 */
		public function set panY( py:Number ):void {
			content.y = Math.max(0, Math.min(py, 1)) * -deltaY;
		}

		
		/**
		 * returns the width difference of the pane and its content
		 */
		public function get deltaX( ):Number {
			return Math.max(0, content.width - width);
		}

		
		/**
		 * returns the height difference o fthe pane and its content
		 */
		public function get deltaY( ):Number {
			return Math.max(0, content.height - height);
		}
		
		
		/**
		 * returns the proportion of the pane height and its content height
		 */
		public function get contentHeightProportion():Number {
			return height / content.height;
		}
		
		
		/**
		 * returns the proportion of the pane width and its content width
		 */
		public function get contentWidthProportion():Number {
			return width / content.width;
		}

		
		public override function get width():Number {
			return masker.width;
		}

		
		public override function get height():Number {
			return masker.height;
		}

		
		public override function set height( h:Number ):void {
			masker.height = h;
		}

		
		public override function set width( w:Number ):void {
			masker.width = w;
		}
	}
}
